#include "comsettingdlg.h"
#include "ui_comsettingdlg.h"
#include "parametersettings.h"

int ComSettingDlg::baudRateTab[] = {1200,2400,4800,9600,19200,38400,57600,115200};
int ComSettingDlg::dataBitsTab[] = {8,7};
int ComSettingDlg::parityTab[] = {QSerialPort::NoParity,QSerialPort::OddParity,QSerialPort::EvenParity};
int ComSettingDlg::stopBitsTab[] = {QSerialPort::OneStop,QSerialPort::TwoStop};
ComSettingDlg::ComSettingDlg(MySerialport* sp, QWidget *parent) :
    QDialog(parent),mSp(sp),
    ui(new Ui::ComSettingDlg)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    //connect(this,SIGNAL(accepted()),this,SLOT(onAccept()));
    this->setGeometry(WINDOW_GEOMETRY_SHIFT(0.5,0.6,0));
//    ui->cbx_braud->setCurrentIndex(ui->cbx_braud->findText(QString::number(mSp->getBaudRate())));
//    ui->cbx_databit->setCurrentIndex(ui->cbx_databit->findText(QString::number((int)mSp->getDataBits())));
//    ui->cbx_flowcontrol->setCurrentIndex(ui->cbx_flowcontrol->findText(mSp->getFlowCtrlStr()));
//    ui->cbx_parity->setCurrentIndex(ui->cbx_parity->findText(mSp->getParityStr()));
//    ui->cbx_stopbit->setCurrentIndex(ui->cbx_stopbit->findText(QString::number((int)mSp->getStopBits())));
    initCbxs();
}

ComSettingDlg::~ComSettingDlg()
{
    delete ui;
}

void ComSettingDlg::initCbxs()
{
    QStringList baudlst = MySerialport::getBaudRateList();
    QStringList datalst = MySerialport::getDataBitsList();
    QStringList paritylst = MySerialport::getParityList();
    QStringList flowlst = MySerialport::getFlowCtrlList();
    QStringList stoplst = MySerialport::getStopBitsList();

    ui->cbx_braud->addItems(baudlst);
    ui->cbx_databit->addItems(datalst);
    ui->cbx_parity->addItems(paritylst);
    ui->cbx_flowcontrol->addItems(flowlst);
    ui->cbx_stopbit->addItems(stoplst);;

    ui->cbx_braud->setCurrentIndex(baudlst.indexOf(mSp->getBaudRateStr()));
    ui->cbx_databit->setCurrentIndex(datalst.indexOf(mSp->getDataBitsStr()));
    ui->cbx_parity->setCurrentIndex(paritylst.indexOf(mSp->getParityStr()));
    ui->cbx_flowcontrol->setCurrentIndex(flowlst.indexOf(mSp->getFlowCtrlStr()));
    ui->cbx_stopbit->setCurrentIndex(stoplst.indexOf(mSp->getStopBitsStr()));
}

void ComSettingDlg::on_btn_cancel_clicked()
{
    this->reject();
}

void ComSettingDlg::on_btn_ok_clicked()
{
    QStringList setNameList;
    setNameList<<ParameterSettings::BAUDRATE<<ParameterSettings::DATABITS<<ParameterSettings::FLOWCTRL
              <<ParameterSettings::PARITY<<ParameterSettings::STOPBITS;
    QVariantList valueList;
    int baudrate = baudRateTab[ui->cbx_braud->currentIndex()];
    int databits = dataBitsTab[ui->cbx_databit->currentIndex()];
    int flowCtrl = 0;
    int parity = parityTab[ui->cbx_parity->currentIndex()];
    int stopbits = stopBitsTab[ui->cbx_stopbit->currentIndex()];
    valueList<<baudrate<<databits<<flowCtrl<<parity<<stopbits;
    ParameterSettings::saveSettingValue(setNameList,valueList,mSp->getSettingGroupName());

    mSp->initComSetting(baudrate,databits,flowCtrl,parity,stopbits);
    emit quitWithText(QString("%1,%2,%3,%4,%5").arg(mSp->getBaudRateStr()).arg(mSp->getDataBitsStr())
                      .arg(mSp->getParityStr()).arg(mSp->getFlowCtrlStr()).arg(mSp->getStopBitsStr()));
    this->accept();
}
